﻿using Heima.OA.BLL;
using Heima.OA.Common;
using Heima.OA.IBLL;
using Heima.OA.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Heima.OA.UI.Portal.Controllers
{
    public class UserInfoController : BaseController
    {
        //UserInfoService userInfoService = new UserInfoService();
        // GET: UserInfo
        public IUserInfoService UserInfoService {get;set;}
        public IRoleInfoService RoleInfoService { get; set;}
        public IActionInfoService ActionInfoService { get; set; }
        public IR_UserInfo_ActionInfoService R_UserInfo_ActionInfoService { get; set; }
        public string Index(PaginationQuery paginationQuery)
        {
            //UserInfoService.GetEntities(u => u.DelFlag == delflagNormal)
            int total = 0;
            var userList = UserInfoService.GetPageEntities(paginationQuery.pagesize, paginationQuery.pageindex, out total, u => u.DelFlag == delflagNormal,
                s => s.SubTime,  true).AsQueryable();

            PaginationData data = new PaginationData() { total = total, rows = JsonHandler.ToJson(userList) };
            JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(data));

            return JsonHandler.ToJson(msg);
        }

        #region cud
        public string GetModel(int id)
        {
            UserInfo userInfo = UserInfoService.GetModel(id);
            string res = string.Empty;
            if (userInfo == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "获取错误!"));
            }
            else
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "获取成功!", JsonHandler.ToJson(userInfo)));
            }
            return res;
        }
        [HttpGet]
        public string Create(UserInfo userInfo)
        {
            userInfo.DelFlag = (short)Heima.OA.Model.DelFlagEnum.Normal;
            userInfo.ModifiedOn = CDateTime.GetDefaultDateTime();
            userInfo.SubTime = DateTime.Now;

            UserInfoService.Add(userInfo);
            JsonMessage message = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "添加用户成功!");
            return JsonHandler.ToJson(message);
        } 
        public string Edit(UserInfo userInfo)
        {
            UserInfo editUserInfo = UserInfoService.GetModel(userInfo.Id);
            string res = string.Empty;
            if(editUserInfo == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "编辑错误!"));
            }
            else
            {
                editUserInfo.UName = userInfo.UName;
                editUserInfo.Pwd = userInfo.Pwd;
                editUserInfo.ShowName = userInfo.ShowName;
                editUserInfo.Remark = userInfo.Remark;
                editUserInfo.ModifiedOn = DateTime.Now;
                bool success= UserInfoService.Update(editUserInfo);
                if (success)
                {
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "编辑成功!")); 
                }
            }

            return res;
        }
        public string Delete(int id)
        {
            UserInfo userInfo = UserInfoService.GetModel(id);
            string res = string.Empty;
            if (userInfo == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "删除错误!"));
            }
            else
            {
                userInfo.DelFlag = (int)DelFlagEnum.Deleted;
                bool success = UserInfoService.Update(userInfo);
                if (success)
                {
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "删除成功!"));
                }
            }
            return res;
        }
        #endregion

        #region 用户设置角色
        //public ActionResult SetRole(int id)
        //{
        //    UserInfo userInfo = UserInfoService.GetModel(id);
        //    ViewBag.AllRoles = RoleInfoService.GetEntities(u => u.DelFlag == delflagNormal).ToList();
        //    return View(userInfo);
        //}
        public string ProcessSetRole(int uid, string roleIds)
        {
            //当前用户id
            //所有打上对勾的角色
            string res = string.Empty;
            if (!string.IsNullOrEmpty(roleIds))
            {
                List<int> roleList = roleIds.Split(',').Select(p => DataConvert.GetInt(p)).ToList();
                UserInfoService.SetRole(uid, roleList);
                JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "设置成功!");
                res = JsonHandler.ToJson(msg);
            }
            else
            {
                JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "角色为空，设置失败!");
                res = JsonHandler.ToJson(msg);
            }

            return res;
        }

        #endregion

        #region 设置特殊权限
        //public string SetAction(int id)
        //{
        //    UserInfoService.GetEntities(u => u.Id == id).FirstOrDefault();
        //    ActionInfoService.GetEntities(a => a.DelFlag == delflagNormal).ToList();
        //    return "";
        //}
        /// <summary>
        /// 删除用户特殊权限
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="actionId"></param>
        /// <returns></returns>
        public string DeleteUserAction(int uid, int actionId)
        {
            var rUserAction = R_UserInfo_ActionInfoService.GetEntities(r => r.ActionInfoId == actionId && r.UserInfoId == uid && r.DelFlag == delflagNormal).FirstOrDefault();
            if (rUserAction != null)
            {
                //rUserAction.DelFlag = (short)DelFlagEnum.Deleted;
                R_UserInfo_ActionInfoService.DeleteListByLogical(new List<int>() { rUserAction.Id });
            }
            return JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "删除成功!"));
        }
        /// <summary>
        /// 设置用户特殊权限
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="actionId"></param>
        /// <param name="value">1:允许,0:拒绝</param>
        /// <returns></returns>
        public string SetUserAction(int uid, int actionId, int value)
        {
            var rUserAction = R_UserInfo_ActionInfoService.GetEntities(r => r.ActionInfoId == actionId && r.UserInfoId == uid && r.DelFlag == delflagNormal).FirstOrDefault();
            if (rUserAction != null)
            {
                rUserAction.HasPermission = (short)value;
                R_UserInfo_ActionInfoService.Update(rUserAction);
            }
            else
            {
                R_UserInfo_ActionInfo rUserInfoActionInfo = new R_UserInfo_ActionInfo();
                rUserInfoActionInfo.ActionInfoId = actionId;
                rUserInfoActionInfo.UserInfoId = uid;
                rUserInfoActionInfo.HasPermission = (short)value;
                rUserInfoActionInfo.DelFlag = delflagNormal;
                R_UserInfo_ActionInfoService.Add(rUserInfoActionInfo);
            }
            return JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "设置用户特殊权限成功!"));
        }

        #endregion
        #region 用户设置部门

        public string SetUserDept(int userId, string deptIds)
        {
            JsonMessage msg = new JsonMessage();
            if (!string.IsNullOrEmpty(deptIds))
            {
                List<int> deptList = deptIds.Split(',').Select(p => DataConvert.GetInt(p)).ToList();
                UserInfoService.SetDept(userId, deptList);
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "用户设置部门成功!");
            }
            else
            {
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "部门为空!");
            }
            return JsonHandler.ToJson(msg);
        }

        #endregion

    }
}